/**
 * 成交统计表
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var angular = require("angular");
    require("angular-ui");
    require("authorize");
    require("permission");
    require("angular-interceptor");
    require("pagination");
    var util = require("util");
    require("./common");
    require('global');

    var showTableRows = 10;//表格默认展示行数

    var api_prefix = '/api/index.php?r=statistics/qmyx-statistics/';

    var app = angular.module('app', ['ngUI', 'ngInterceptor', 'commonModule']);

    // 查询服务
    function queryService($http, $q, baseData) {
        var service = {
            detailData: undefined,
            detialTotal: undefined,
            detailPageIndex: 1,
            detailPageSize: 10,
            showDetail: false,
            hasData: undefined,
            hasDetailData: undefined,
            query: function () {
                service.loading = true;
                service.showDetail = false;
                var filters = _.pick(baseData.getFilters(), 'start_time', 'end_time');
                var params = {
                    proj_ids: baseData.projectIds.join(),
                    start_date: filters.start_time,
                    end_date: filters.end_time
                };
                return $http.post(api_prefix + 'get-broker-trade-brokerage-statistics', params).success(function (res) {

                    if (res.retCode == 0) {
                        service.data = res.data.data;
                        service.summaryDetailData = res.data.summary;
                        service.hasData = !!res.data.data.length;

                    }
                    service.loading = false;
                });
            },
            queryDetail: function (page) {
                service.loading = true;
                this.detailPageIndex = page || 1;
                var filters = _.pick(baseData.getFilters(), 'start_time', 'end_time');
                var params = {
                    proj_ids: baseData.projectIds.join(),
                    proj_id: this.proj_id,
                    filter_column: this.filter_column,
                    pageIndex: page - 1 || 0,
                    pageSize: this.detailPageSize
                };
                params.start_date = filters.start_time;
                params.end_date = filters.end_time;
                return $http.post(api_prefix + 'get-broker-trade-brokerage-details', params).success(function (res) {
                    if (res.retCode == 0) {
                        service.detailData = res.data.items;
                        service.detailTotal = res.data.total;
                        service.hasDetailData = !!res.data.items.length;
                    }
                    service.loading = false;
                })
            },
            //检查详情报表是否需要导出任务
            checkNeedTask: function (params) {
                var taskId = false;
                $.ajax({
                    type: 'POST',
                    async: false,
                    data: JSON.stringify(params),
                    contentType: 'application/json;charset=UTF-8',
                    url: api_prefix + "check-export-detail-need-task&report=broker_trade_brokerage&token=" + util.getToken(),
                    success: function (re) {
                        if (re.data.needTask) {
                            taskId = re.data.taskId;
                        }
                    },
                    error: function () {

                    }
                });
                return taskId;
            },

            //输出经纪人统计明细表到Excel
            exportDetailsData: function (e) {
                var filters = {
                    start_date: baseData.startDate,
                    end_date: baseData.endDate,
                    proj_ids: baseData.projectIds.join(),
                    proj_id: this.proj_id,
                    filter_column: this.filter_column
                };
                var task = this.checkNeedTask(filters),
                    downloadURL = api_prefix + 'export-broker-trade-brokerage-details';
                if (!task) {
                    util.download(downloadURL,filters);
                } else {
                    window.open('/page/export_data.shtml?taskId=' + task)
                }

            },

            //输出经纪人统计表到Excel
            exportData: function () {
                var filters = {
                    start_date: baseData.startDate,
                    end_date: baseData.endDate,
                    proj_ids: baseData.projectIds.join()
                };
                var url = api_prefix + 'export-broker-trade-brokerage-statistics';
                util.download(url,filters);

            }

        };

        return service;
    }

    app.factory('query', ['$http', '$q', 'baseData', queryService]);

    function StatisticsController($scope, $location, baseData, query) {
        $scope.baseData = baseData;
        $scope.query = query;
        $scope.showTableRows = showTableRows;

        $scope.showDetailsData = function (data, dialog, dialogScope, col) {
            dialogScope.query = query;
            dialogScope.query.proj_id = data.proj_id || '';
            dialogScope.query.filter_column = col;
            dialogScope.query.detailPageSize = 10;
            query.queryDetail().then(function(res){
                dialog.showModal();
                setTimeout(function () {
                    dialog.reset()
                }, 15)
            });
            return false;
        };

        //展开 收起
        $scope.toggleMoreRecord = function () {
            $scope.query.showDetail = !$scope.query.showDetail;
            $("table tr:gt("+showTableRows+")").toggleClass('hideRecords',!$scope.query.showDetail)
        };
    }

    app.controller('StatisticsController', ['$scope', '$location', 'baseData', 'query', StatisticsController]);

    function SearchController($scope, $location, baseData, query) {
        $scope.baseData = baseData;
        $scope.query = query;
        $scope.$watch('baseData.projectIds', function (newValue) {
            if (newValue !== undefined && baseData.loaded) {
                query.query();
            }
        });

    }

    app.controller('SearchController', ['$scope', '$location', 'baseData', 'query', SearchController]);

    angular.bootstrap(document.getElementById('ng-app'), ['app']);
});